package cn.monkey.family.data.crud;

import cn.monkey.family.data.*;
import cn.monkey.family.data.context.RequestContext;
import cn.monkey.family.data.crud.repository.QueryRequestRepository;

import java.util.Collection;
import java.util.List;

public abstract class AbstractEntityCrudService<Q extends QueryRequest, T extends Entity, D, V extends EntityVo, MAPPER extends EntityMapper<D, T, V>,
        REPOSITORY extends QueryRequestRepository<T, String, Q>> extends AbstractCrudService<String, Q, T, D, V, MAPPER, REPOSITORY> {
    public AbstractEntityCrudService(REPOSITORY repository, MAPPER mapper) {
        super(repository, mapper);
    }

    public AbstractEntityCrudService(REPOSITORY repository) {
        super(repository);
    }

    @Override
    public Result<Void> delete(RequestContext requestContext, String s) {
        this.repository.findById(s)
                .map(t -> {
                    t.setEnabled(Boolean.FALSE);
                    return t;
                })
                .ifPresent(this.repository::save);
        return Results.ok();
    }

    @Override
    public Result<Void> deleteAll(RequestContext requestContext,Collection<String> strings) {
        List<T> list = this.repository.findAllById(strings)
                .stream().peek(t -> t.setEnabled(Boolean.FALSE))
                .toList();
        this.repository.saveAll(list);
        return Results.ok();
    }
}
